package com.example.demo.mapper;

import com.example.demo.controller2.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2024-02-27
 * Time: 16:02
 */
@Mapper//
public interface UserIntface {
    @Options(useGeneratedKeys = true, keyProperty = "id")//设施id为自增主键
    @Insert("insert into userinfo(username, password, age phone) " +
            "value(#{username}, #{password}, #{phone})")//#后面的是user里的字段
    Integer isTure(User user);//返回影响的行数

    @Insert("insert into userinfo(username, password, age phone) " +
            "value(#{user.username}, #{user.password}, #{user.phone})")//#后面的是user里的字段
    Integer isTure2(@Param("user") User user);

   // @Update("update userinfo set age = #{age} where id = #{id}")
   // @Delete("delete form userinfo where id = #{id}")

    //使用@Results
    @Results({
            @Result(column = "username1", property = "username"),
            @Result(column = "password1", property = "password"),//倘若数据库的字段和Java的字段不同
            @Result(column = "age1", property = "age"),//前面的是数据库
    })
    @Select("select * from userinfo")
    List<User> list();

    //复用@Results

    @Results(id = "map", value = {
            @Result(column = "username1", property = "username"),
            @Result(column = "password1", property = "password"),//倘若数据库的字段和Java的字段不同
            @Result(column = "age1", property = "age"),//前面的是数据库
    })

    @ResultMap(value = "map")
    @Select("select * fom userinfo where id = #{id}")//当只有一个参数，参数名可以任意
    User getuserone(Integer id);

    //${}是即时sql，它是直接将提供的参数拼接到sql语句上,如select * fom userinfo where username = admin, 参数为String，有sql注入
    //#{}是预编译sql,它是会将参数进行转换在拼接到sql语句上, 如select * fom userinfo where username = ‘admin’，参数为String,性能高

    @Insert("<script>" +
            "insert into userinfo" +
            "<tirm prefix = '(' suffix = ')' suffixOverrides = ','>" +
            "<if username != null> username, </if>" +
            "<if password != null> password, </if>" +
            "<if age != null> age, </if>" +
            "</tirm>" +
            "value" +
            "<tirm prefix = '(' suffix = ')' suffixOverrides = ','>" +
            "<if username != null> #{username},</if> " +
            "<if password != null> #{password},</if> " +
            "<if age != null> #{age} </if> " +
            "</tirm>" +
            "</script>"
            )
    Integer isTure3(User user);

    @Select("<script>" +
            "select username, password, age phone fom userinfo " +
            "<where>" +
            "<if username != null> and username = #{username} </if>" +
            "<if password != null> and password = #{password} </if>" +
            "<if age != null> age = #{age} and </if>" +
            "</where>" +
            "</script>")//当只有一个参数，参数名可以任意
    User getuserone2(Integer id);

    @Update("<script>" +
            "update userinfo " +
            "<set>" +
            "<if age != null> age = #{age}, </if>" +
            "</set>" +
            "where username = #{username}" +
            "/<script>")
    Integer update(User user);

    @Delete("<script>" +
            "delete form userinfo where id in " +
            "<foreach collection = 'lists' item = 'id' open = '(' close = ')' separator = ','>" +
            "#{id}" +
            "</foreach>" +
            "<script>")
    Integer delete(List<Integer> lists);

}
